﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

using IssueTracker.Business.Entity;  

namespace IssueTracker.Data
{
    public class ProjectDataAccess: DataAccessComponent
    {
        private IssueTrackerDBDataContext dc;

        public Project Create(Project project)
        {
            Project temp = base.Clone<Project>(project);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;

                dc.Projects.InsertOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();

                    project = base.Clone<Project>(temp);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return project;           
            
        }

        public Project Retrieve(int id)
        {

            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.Projects
                           where proj.Id == id
                           select proj;

                if (list.Count<Project>() > 0)
                {
                    Project obj = list.First<Project>();
                    return obj;
                }
                else
                    return null;
            }
        }

        public Project Update(Project project)
        {

            Project temp = base.Clone<Project>(project);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;
                dc.Projects.Attach(temp, true);

                try
                {
                    dc.SubmitChanges();

                    project = base.Clone<Project>(temp);
                }
                catch (ChangeConflictException ccex)
                {
                    throw new ApplicationException("The project record that you are trying to update maybe out-dated. Please refresh and try again.", ccex);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return project;
            
        }

        public void Delete(Project project)
        {

            Project temp = base.Clone<Project>(project);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.Projects.Attach(temp);

                dc.Projects.DeleteOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<Project> List()
        {
            IEnumerable<Project> returnList = null;

            using (dc = new IssueTrackerDBDataContext())
            {
                returnList = dc.Projects;

                if (returnList != null)
                    return returnList.ToList();
                else
                    return null;
            }
        }

        
    }
}
